<template>
	<el-container>
		<el-aside width="240px">
			<el-container>
				<el-main class="nopadding">
					<blCategoryTree model="MsgTemplate" title="订阅消息模板" ref="group" @node-click="groupClick" />
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add">新增订阅消息</el-button>
					<div class="right-panel-search" style="padding-left: 15px;">
						<el-input v-model="search.keyword" placeholder="订阅消息名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch">查询</el-button>
					</div>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">

					</div>
				</div>
			</el-header>
			<div>
				<el-card shadow="never">
					<sc-select-filter :data="selectFilter" :selected-values="selectedValues" :label-width="80"
						@on-change="stateChange"></sc-select-filter>
				</el-card>
			</div>
			<el-main class="nopadding">
				<taTableCard hidePagination hideAdd rowHeight="auto" :cardSize="[24,24,24,24,24]" ref="table"
					:apiObj="apiObj.list" row-key="id">
					<template #default="{item}">
						<div style="display: flex;flex-direction: column;">
							<div class="msg-template-cate">
								{{item.cat_name}}
							</div>

							<div class="msg-template-list">
								<div class="msg-template-item" v-for="(data,index) in item.msg_templates"
									:key="data.id">
									<div class="msg-template-title">
										<div @click="table_edit(data)">
											{{data.msg_name}}
										</div>
										<div style="color: #ccc;">
											{{index+1}}#
										</div>
									</div>

									<div class="msg-template-status">
										<div>是否启用</div> <el-switch disabled v-model="data.is_enabled" inline-prompt
											active-text="启用" inactive-text="禁用" size="large" :active-value="true"
											:inactive-value="false"></el-switch>
									</div>
									<div style="margin-bottom: 15px;">
										<el-descriptions column="1" border>
											<el-descriptions-item align="right" label-align="left"
												v-for="(map,index) in data.msg_sub_template_mapping">
												<template #label>
													<div style="color: rgb(64, 158, 255);">
														{{map.channel}}
													</div>
												</template>
												<div>
													{{map.name}}
												</div>
											</el-descriptions-item>
										</el-descriptions>
									</div>


									<div @click="copyText(data.msg_template_code)"
										style="font-size: 14px;line-height: 14px;color: #ccc;margin-bottom: 15px;">
										{{data.msg_template_code}}
									</div>
								</div>
							</div>
						</div>
					</template>
				</taTableCard>

			</el-main>
		</el-container>
	</el-container>

	<uploadDialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload=false">
	</uploadDialog>

	<saveDialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false">
	</saveDialog>

</template>

<script>
	import saveDialog from './save'
	import uploadDialog from './upload'
	import previewDialog from './save'
	import scSelectFilter from '@/components/scSelectFilter'

	export default {
		name: 'msgTemplate.manage',
		components: {
			saveDialog,
			uploadDialog,
			scSelectFilter
		},
		data() {
			return {
				medieType: '全部',
				dialogVisible: true,
				selectFilter: [{
					title: "服务对象",
					key: "msg_type",
					options: [{
							label: "全部",
							value: null
						},
						{
							label: "客户消息",
							value: "客户",
						},
						{
							label: "员工消息",
							value: "员工",
						}
					]
				}],
				selectedValues: {
					type: [null],
				},
				dialog: {
					save: false,
					upload: false
				},
				apiObj: this.$API.system.msgTemplate,
				selection: [],
				search: {
					keyword: null,
					dateRange: null
				}
			}
		},
		mounted() {

		},
		methods: {
			async copyText(text) {
				try {
					await navigator.clipboard.writeText(text);
					this.$message.success('复制成功！');
				} catch (e) {

				}
			},
			handleQuanXuan() {

				this.$refs.table2.toggleAllSelection()

				console.error(this.$refs.table2.selectedRows)

			},
			async handleCopy(text) {
				try {
					await navigator.clipboard.writeText(text);
					this.$message.success("复制成功！");
				} catch (err) {
					this.$message.error("复制失败，请手动复制");
				}
			},
			stateChange(e) {
				var params = {
					selectFilter: e
				}
				this.$refs.table.upData(params)
			},
			// 上传素材
			add() {
				this.dialog.save = true;
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.saveDialog.open().setData({
						category_id: checkedNode?.id || null
					});
				})
			},
			table_preview(row) {
				this.dialog.preview = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('preview').setData(row)
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row);
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index) {

				return this.$alert("开发中...", "提示", {
					type: 'error'
				});

				var reqData = {
					id: row.id
				}
				var res = await this.$API.system.media.remove.post(reqData);
				if (res.code == 0) {
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					category_id: data.id
				}
				this.$refs.table.upData(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.msg-template-cate {
		font-size: 24px;
		font-weight: bold;
		margin-bottom: 10px;
		padding-bottom: 5px;
		border-bottom: 1px solid #ccc;
	}

	.msg-template-list {
		display: flex;
		flex-direction: row;
		gap: 15px;

		.msg-template-item {
			background-color: #f5f5f5;
			border-radius: 8px;
			min-height: 150px;
			width: 340px;
			padding-left: 15px;
			padding-right: 15px;

			.msg-template-title {
				font-size: 18px;
				font-weight: bold;
				height: 50px;
				line-height: 50px;

				display: flex;
				flex-direction: row;
				justify-content: space-between;
			}

			.msg-template-status {
				display: flex;
				font-size: 14px;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
			}
		}
	}


	.media-dropdown {
		position: absolute;
		right: -17px;
		bottom: -10px;
		display: none;
	}

	:deep(.el-card__body:hover) .media-dropdown {
		display: block;
	}

	:deep(.el-card:has(.mediaSelect)) {
		border: 2px solid #409eff;
	}
</style>
